package com.yjy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.system.comm.util.JsonUtils;
import com.system.common.pojo.SysMenu;
import com.system.common.pojo.SysUser;
import com.yjy.service.SysMenuService;
import com.yjy.service.SysRoleService;

@Controller
@RequestMapping("menu")
public class SysMenuController {
	
	@Autowired
	private SysMenuService menuService;
	
	@Autowired
	private SysRoleService roleService;
	
	@RequestMapping("list")
	public String list(Model model,String mname,HttpServletRequest request,HttpSession session){
		Map<String, Object>  map=new HashMap<>();
		if (mname!=null && !mname.equals("")) {
			map.put("mname", mname);
			request.setAttribute("mname", mname);
		}
		List<SysMenu> list=menuService.listAll(map);
		SysUser user=(SysUser) session.getAttribute("user");		
		List<SysMenu> power=roleService.selectPower(user.getUserId());
		String menus="";
		for (SysMenu menu : power) {
			menus+=menu.getUrl()+",";
		}
		model.addAttribute("power", menus);
		model.addAttribute("menuList", list);
		return "sys/sysmenu";
	}
	
	@RequestMapping(value="tooMenu",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String tooMenu(HttpServletResponse response){
		 Map<String, Object>  map=new HashMap<>();
		 List<SysMenu>  menus=menuService.listAll(map);		
		 SysMenu m=new SysMenu();
		 m.setMenuId("0");
		 m.setCascadeId("0");
		 m.setMenuName("功能菜单");
		 m.setMenuType("1");
		 m.setParentId("-1");
		 m.setIsAutoExpand("1"); 
		 m.setStatus("1");
		 m.setEditMode("1");
		 m.setSortNo(2);
		 menus.add(m);
		 String menu= JsonUtils.toJson(menus);
		 return menu;
	}
	@RequestMapping("delete")
	public String delete(String menuid,Model model){
		menuService.delete(menuid);
	    return "redirect:/menu/list";
		
	}
	
	@RequestMapping("queryMenuIds")
	public String queryDeptIds(String ids,Model model){
	    List<SysMenu>  menus= menuService.queryMenuIds(ids);
	    model.addAttribute("menus", menus);
		return "sys/sysmenu";
		
	}
	@RequestMapping("save")
	public String save(SysMenu menu,HttpSession session){
	   SysUser user= (SysUser) session.getAttribute("user");
	   menu.setCreateUserId(user.getUserId());
	   menuService.save(menu);
		return "redirect:/menu/list";
		
	}
	@RequestMapping(value="queryParentId",produces = "application/json;charset=utf-8")
	@ResponseBody
	public String queryParentId(){
	   List<SysMenu>  menus=menuService.queryParentId();
	   SysMenu m=new SysMenu();
		 m.setMenuId("0");
		 m.setCascadeId("0");
		 m.setMenuName("功能菜单");
		 m.setMenuType("1");
		 m.setParentId("-1");
		 m.setIsAutoExpand("1"); 
		 m.setStatus("1");
		 m.setEditMode("1");
		 m.setSortNo(2);
		 menus.add(m);
	   String jsonMenus=JsonUtils.toJson(menus);
		return jsonMenus;
		
	}	
	
	@RequestMapping(value="toselectParentname",produces="application/json;charset=utf-8")
	@ResponseBody
	public String toselectParentname(String parentId){
		String menuName=menuService.toselectParentname(parentId);
		return menuName;
	}
	
	
	@RequestMapping("update")
	public String update(SysMenu menu,HttpSession session){
		SysUser user=(SysUser) session.getAttribute("user");
		menu.setModifyUserId(user.getUserId());
		menuService.update(menu);
		return "redirect:/menu/list";
	}
}
